package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Commodity;
import cn.wolfcode.trip.base.domain.Shop;
import cn.wolfcode.trip.base.mapper.CommodityMapper;
import cn.wolfcode.trip.base.mapper.ShopMapper;
import cn.wolfcode.trip.base.query.ShopQueryObject;
import cn.wolfcode.trip.base.service.IShopService;
import cn.wolfcode.trip.base.service.IShopService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShopServiceImpl implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private CommodityMapper commodityMapper;


    @Override
    public PageInfo query(ShopQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Shop> list = shopMapper.selectForList(qo);
        return new PageInfo(list);
    }

    @Override
    public void saveOrUpdate(Shop shop) {
        shopMapper.updateByState(shop);
    }

    @Override
    public void buyProduct(Integer money, Long userid) {
        Shop shop = shopMapper.selectByUserid(userid);
        if (shop.getTotalIntegral()>money) {
            shopMapper.spendMoney(money,userid);
        }
    }

    @Override
    public Object getMoneyByuserid(Long userid) {
        return shopMapper.selectByUserid(userid);
    }

    @Override
    public void carbuyProduct(int totalPrice, Long userid, Long[] ids) {
        Shop shop = shopMapper.selectByUserid(userid);
        if (shop.getTotalIntegral()>totalPrice) {
            shopMapper.spendMoney(totalPrice,userid);
            commodityMapper.normalstate(ids);
        }
    }


}
